/**
 * 
 */
package com.alkandros.myloke.activities;

import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.alkandros.myloke.R;
import com.alkandros.myloke.constants.IAppConstants;
import com.alkandros.myloke.managers.ApiManager;
import com.alkandros.myloke.managers.CustomProgressDialog;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMyLocationButtonClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;

/**
 * @author Jiju Induchoodan
 * 
 */
public class BaseActivity extends FragmentActivity implements
		ConnectionCallbacks, OnConnectionFailedListener, LocationListener,
		OnMyLocationButtonClickListener,IAppConstants {
	
	protected CustomProgressDialog progress;
	protected GoogleMap mMap;
	protected final LatLngBounds BOUNDS = new LatLngBounds(new LatLng(53.163844, 2),new LatLng(54.342353, -1) );
	protected final int MAX_ZOOM = 18;
	protected final int MIN_ZOOM = 14;
	protected LocationClient mLocationClient;
	protected ApiManager apiManager;
	protected Location location;
	private int isLocationObjectCreated=1;
	protected LocationCallBack locationCallBack;
	protected interface LocationCallBack  
	{
		void locationObjectReceived(Location location);
	}
	
	// These settings are the same as the settings for the map. They will in
	// fact give you updates
	// at the maximal rates currently possible.
	protected static final LocationRequest REQUEST = LocationRequest.create()
			.setInterval(50000) // 5 seconds 5000
			.setFastestInterval(120000) // 16ms = 60fps
			.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

	@Override
	public boolean onMyLocationButtonClick() {
		return false;
	}

	@Override
	public void onLocationChanged(Location loc) {
		location=loc;
		if(mMap!=null)
		{
			mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(loc.getLatitude(), loc.getLongitude()),13));
			locationCallBack.locationObjectReceived(location);
		}
		
	//	mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(adjustBoundsForMaxZoomLevel(BOUNDS), 20));
		
	//	if(isLocationObjectCreated==1)
	//	{
			
	//		isLocationObjectCreated=isLocationObjectCreated+1;
	//	}
	}

	@Override
	public void onConnectionFailed(ConnectionResult arg0) {
	
	}
	@Override
	public void onPause() {
		super.onPause();
		if (mLocationClient != null) {
			mLocationClient.disconnect();
		}
	}
	@Override
	protected void onResume() {
		super.onResume();
		setUpMapIfNeeded();
		setUpLocationClientIfNeeded();
		mLocationClient.connect();
	}
	@Override
	public void onConnected(Bundle arg0) {
		mLocationClient.requestLocationUpdates(REQUEST, this); // LocationListener
	}
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		locationCallBack = (LocationCallBack) this;
		progress  = CustomProgressDialog.show(this, false);
	}

	@Override
	public void onDisconnected() {

	}
	private void setUpMapIfNeeded() {
		// Do a null check to confirm that we have not already instantiated the
		// map.
		if (mMap == null) {
			// Try to obtain the map from the SupportMapFragment.
			mMap = ((SupportMapFragment) getSupportFragmentManager()
					.findFragmentById(R.id.map)).getMap();

			// mMap = ((MapFragment) getFragmentManager().findFragmentById(
			// R.id.map)).getMap();
			// Check if we were successful in obtaining the map.
			if (mMap != null) {
				mMap.setMyLocationEnabled(true);
				mMap.setOnMyLocationButtonClickListener(this);
				
				

				/*
				 * // mMap.setMapType(GoogleMap.MAP_TYPE_NONE); TileProvider
				 * provider = new TileProvider() {
				 * 
				 * @Override public Tile getTile(int arg0, int arg1, int arg2) {
				 * // TODO Auto-generated method stub return null; } }; //
				 * mMap.addTileOverlay(new //
				 * TileOverlayOptions().tileProvider(provider)); // CameraUpdate
				 * upd = CameraUpdateFactory.newLatLngZoom(new //
				 * LatLng(41.87145, 12.52849), 14); // mMap.moveCamera(upd);
				 * mOverscrollHandler.sendEmptyMessageDelayed(0, 100);
				 */
			}
		}
	}
	private void setUpLocationClientIfNeeded() {
		if (mLocationClient == null) {
			mLocationClient = new LocationClient(getApplicationContext(), this, // ConnectionCallbacks
					this); // OnConnectionFailedListener
		}
	}
	
	private LatLngBounds adjustBoundsForMaxZoomLevel(LatLngBounds bounds) {
		  LatLng sw = bounds.southwest;
		  LatLng ne = bounds.northeast;
		
		  double deltaLat = Math.abs(sw.latitude - ne.latitude);
		  double deltaLon = Math.abs(sw.longitude - ne.longitude);

		  final double zoomN = 0.005; // minimum zoom coefficient
		  if (deltaLat < zoomN) {
		     sw = new LatLng(sw.latitude - (zoomN - deltaLat / 2), sw.longitude);
		     ne = new LatLng(ne.latitude + (zoomN - deltaLat / 2), ne.longitude);
		     bounds = new LatLngBounds(sw, ne);
		  }
		  else if (deltaLon < zoomN) {
		     sw = new LatLng(sw.latitude, sw.longitude - (zoomN - deltaLon / 2));
		     ne = new LatLng(ne.latitude, ne.longitude + (zoomN - deltaLon / 2));
		     bounds = new LatLngBounds(sw, ne);
		  }

		  return bounds;
		}

	

}
